﻿using Microsoft.AspNetCore.Mvc;

using SMARTHOTEL.Products;
using SMARTHOTEL.RoomMoneys;
using SMARTHOTEL.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Users;

namespace SMARTHOTEL.Customers
{
    /// <summary>
    /// 客户信息
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class CustomerService : ApplicationService, ICustomerService
    {
        IRepository<Customer> repository;

        public CustomerService(IRepository<Customer> repository)
        {
            this.repository = repository;
        }
        /// <summary>
        /// 客户信息添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("InsertCustomer")]
        public async Task<ResultDto<bool>> InsertCustomer(CustomerDto dto)
        {
            // 判断用户名是否存在
            var users = await repository.FirstOrDefaultAsync(t => t.CustomerName == dto.CustomerName);
            if (users == null)
            {
                //添加用户表


                Customer customer = ObjectMapper.Map<CustomerDto, Customer>(dto);
                    await repository.InsertAsync(customer);
                    return new ResultDto<bool> { Data = true, Code = StatusCode.Success, Msg = "添加成功" };

            }
            else
            {
                return new ResultDto<bool>
                {
                    Code = StatusCode.Faield,
                    Msg = "添加失败",
                    Data = false
                };
            }
        }



        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="UserPwd"></param>
        /// <returns></returns>
        [HttpGet("CustomerLogin")]
        public async Task<ResultDto<CustomerDto>> CustomerLogin(string UserName, string UserPwd)
        {

            var pwd = Md5(UserPwd);
            var user = await repository.FirstOrDefaultAsync(t => t.CustomerName == UserName);

            if (user == null)
            {
                return new ResultDto<CustomerDto> { Code = StatusCode.Faield, Msg = "用户不存在" };
            }

            if (user.Password != pwd)
            {
                return new ResultDto<CustomerDto> { Code = StatusCode.Faield, Msg = "用户和密码不匹配" };
            }

            //var entity = ObjectMapper.Map<Customer, CustomerDto>(user);
            return new ResultDto<CustomerDto>
            {
                Code = StatusCode.Success,
                Msg = "登录成功",
                //Data = entity
            };
        }
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Md5(string str)
        {
            var md5 = MD5.Create();
            Byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
            return BitConverter.ToString(bytes).Replace("-", "").ToUpper();
        }
    }
}
